package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;
@WebFilter(filterName="/LoginFilter",urlPatterns="/*")
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request1=(HttpServletRequest)request;
		HttpServletResponse reponse1=(HttpServletResponse)response;
		HttpSession session= request1.getSession();
		
		String url=request1.getRequestURL().toString();
		
		Object uname=session.getAttribute("uname");
		
		if(uname==null && url.contains("Login")==false && url.contains("checkLogin")==false){
			//request1.getRequestDispatcher("Login.jsp").forward(request1, reponse1);
			reponse1.sendRedirect(request1.getContextPath()+"/Login");
			return;
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("登录检验过滤器启动");
		
	}

}
